package com.example.mytestapplication;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.example.mytestapplication.utils.PreferencesLoader;

import java.io.IOException;
import java.util.HashMap;

import static android.content.ContentValues.TAG;
import static com.example.mytestapplication.MywidgetProvider.IMG_BTN1;
import static com.example.mytestapplication.MywidgetProvider.IMG_BTN2;

/**
 * Created by wei on 2017/6/23.
 */

public class MyWidgetProvider2 extends AppWidgetProvider {

    private UdpHelper udpHelper;

    /*
            当且仅当第一个Widget被拖动到面上的时候，会调用onEnable方法和onUpdate方法，
            随后再次拖动第二个第三个...Widget到桌面上，仅仅会调用，widget的onUpdate了
             */
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        for(int appWidgetId : appWidgetIds){
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.layout_widget_provider2);
//        Intent clickIntent = new Intent(context, OneKeyOpenActivity.class);
            appWidgetManager.updateAppWidget(appWidgetId,views);
        }
    }

    private void updataWeight(Context context){
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.layout_widget_provider2);
        views.setImageViewResource(R.id.notice_img1,R.drawable.email_1);
        views.setOnClickPendingIntent(R.id.notice_img1,null);
        ComponentName componentName = new ComponentName(context,MyWidgetProvider2.class);
        appWidgetManager.updateAppWidget(componentName,views);
    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        udpHelper = UdpHelper.getInstance(context);
        if(!udpHelper.checkIsConnect()){
            udpHelper.sendThread("qip");
        }
    }

    /*
    当且仅当删除最后一个Widget对象的时候，会调用该widget的onDelete方法和onDisable方法，
    删除数第二个第三个...Widget的时候，仅仅会调用Widget的，onDelete方法。
     */
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
    }

    /*
    随着onEnable，onUpdate，onDelete，onDisable方法的调用都会产生系统广播，从而让Widget的onReceive方法被调用
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        String action = intent.getAction();
        if(action.equals("action_noice_btn1")){
            Toast.makeText(context, "播放语音", Toast.LENGTH_SHORT).show();
            MediaPlayer mediaPlayer = new MediaPlayer();
            try {
                HashMap<String,String> header = new HashMap<>();
                String openid = PreferencesLoader.getAppPreferences().getString("openid", "");
                header.put("openId",openid);
                header.put("Connection", "close");
                header.put("version", "123");
//                mediaPlayer.setDataSource("https://property-liefeng.oss-cn-shenzhen.aliyuncs.com/TEST/voice/hzwy_property/20170814/20170814174932Q7c9mQYZ.aac");
                mediaPlayer.setDataSource(context, Uri.parse("https://property-liefeng.oss-cn-shenzhen.aliyuncs.com/TEST/voice/hzwy_property/20170814/20170814200733pjs4bXSc.aac"),header);
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                    }
                });
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.release();
                    }
                });

            } catch (IOException e) {
                e.printStackTrace();
            }
            updataWeight(context);
        }
    }
}
